package com.haha.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.haha.gulimall.product.entity.AttrEntity;
import com.haha.gulimall.product.service.AttrAttrgroupRelationService;
import com.haha.gulimall.product.service.CategoryService;
import com.haha.gulimall.product.service.impl.AttrServiceImpl;
import com.haha.gulimall.product.vo.AttrGroupRelationVo;
import com.haha.gulimall.product.vo.AttrGroupWhithAttrsVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.haha.gulimall.product.entity.AttrGroupEntity;
import com.haha.gulimall.product.service.AttrGroupService;
import com.haha.common.utils.PageUtils;
import com.haha.common.utils.R;

import javax.annotation.Resource;


/**
 * 属性分组
 *
 * @author xiongweizhong
 * @email xiongwei@gmail.com
 * @date 2022-06-22 23:36:06
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Resource
    private CategoryService categoryService;

    @Resource
    AttrServiceImpl attrService;

    @Resource
    AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId){
      //  PageUtils page = attrGroupService.queryPage(params);
        PageUtils page=  attrGroupService.queryPage1(params,catelogId);
        return R.ok().put("page", page);
    }

    /**
     * /product/attrgroup/{catelogId}/withattr
     * 得到三级分类的所有属性分组，并把属性详情也带出来
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrsByCatId(@PathVariable("catelogId") Long catelogId){
       List<AttrGroupWhithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCatId(catelogId);
       return R.ok().put("data",vos);
    }


    /**
     * 获取分组的所有属性
     * @return
     */
   // /product/attrgroup/{attrgroupId}/attr/relation
    @RequestMapping("/{attrgroupId}/attr/relation")
    public R getAttrRelation(@PathVariable("attrgroupId") Long attrgroupId){
       List<AttrEntity> attrEntities = attrService.getRelationAttr(attrgroupId);

        return R.ok().put("data", attrEntities);
    }

    /**
     * /product/attrgroup/{attrgroupId}/noattr/relation
     * 获取分组还没有关联的属性
     * @return
     */
    @RequestMapping("/{attrgroupId}/noattr/relation")
    public R getAttrNoRelation(@RequestParam Map<String, Object> params,@PathVariable("attrgroupId") Long attrgroupId){
        PageUtils page = attrService.getNoRelationAttr(params,attrgroupId);

        return R.ok().put("page",page);
    }

    /**
     * /product/attrgroup/attr/relation
     * @param attrGroupRelationVo
     * @return
     */
    @PostMapping("/attr/relation")
    public R addAttrGroupRelation(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVo){
        attrAttrgroupRelationService.addAttrGroupRelation(attrGroupRelationVo);
        return R.ok();

    }


    ///product/attrgroup/attr/relation/delete
    @PostMapping("/attr/relation/delete")
    public  R deleteGroupRelation(@RequestBody AttrGroupRelationVo attrGroupRelationVo[]){  //这里用list集合也是可以封装的
        attrGroupService.deleteGroupRelation(attrGroupRelationVo);
        return R.ok();

    }



    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long path []=categoryService.getCategroyPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(path);

        return R.ok().put("attrGroup", attrGroup);
    }



    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
